from django import forms

from users.models import UserProfile
import re
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.core.exceptions import ValidationError

def mobile_validate(value):
    mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    try:
        if mobile_re.match(value) or validate_email(value):
            return True
        else:
            return False
    except ValidationError:
        raise ValidationError('手机号码或邮箱格式错误')


class LoginForm(forms.Form):
    email_mobile = forms.CharField(validators=[mobile_validate,],required=True,min_length=1)
    password = forms.CharField(required=True, min_length=1)



class RegisterForm(forms.Form):
    mobile = forms.CharField(required=True, max_length=11,min_length=11)
    email = forms.EmailField(required=True)
    password = forms.CharField(required=True, min_length=1)
    password2 = forms.CharField(required=True, min_length=1)
    # captcha = CaptchaField(error_messages={"invalid":u"验证码错误"})


class ForgetForm(forms.Form):
    email = forms.EmailField(required=True)
    mobile = forms.CharField(required=True,min_length=11,max_length=11)
    # captcha = CaptchaField(error_messages={"invalid":u"验证码错误"})


class ModifyPwdForm(forms.Form):
    password1 = forms.CharField(required=True, min_length=1)
    password2 = forms.CharField(required=True, min_length=1)


class UploadImageForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['image']


class UserInfoForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['nick_name', 'gender', 'birthday', 'address', 'mobile']